package com.example.springboot.modules.test.service.impl;

import com.example.springboot.modules.common.vo.Result;
import com.example.springboot.modules.test.dao.StudentDao;
import com.example.springboot.modules.test.entity.Student;
import com.example.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    @Transactional
    public Result<Student> insertStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Insert success.",student);
    }

    @Override
    @Transactional
    public Result<Student> updateStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Update success.",student);
    }

    @Override
    @Transactional
    public Result<Object> deleteStudentById(int id) {
        studentDao.deleteById(id);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Delete success.");
    }

    @Override
    public Student getStudentById(int id) {
        return studentDao.getReferenceById(id);
    }


}
